package cn.com.nes.site.module.service.crm;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.com.nes.site.entity.DataReq;
import cn.com.nes.site.entity.PageData;
import cn.com.nes.site.module.dao.crm.AnalysisDaoImpl;

@Service
public class AnalysisService {
	@Resource
    private AnalysisDaoImpl impl;
	
	public PageData getClueStatistics(DataReq req) {
		return impl.getClueStatistics(req);
	}
	
	public PageData getClueSources(DataReq req) {
		return impl.getClueSources(req);
	}
	
	public PageData getClueStagesStatistics(DataReq req) {
		return impl.getClueStagesStatistics(req);
	}
	
	public PageData getClueDistributionStatistics(DataReq req) {
		return impl.getClueDistributionStatistics(req);
	}
	
	public PageData getClueProperiesStatistics(DataReq req) {
		return impl.getClueProperiesStatistics(req);
	}
	
	public PageData getClueStateDateSerialStatistics(DataReq req) {
		PageData pagedata = impl.getClueStateDateSerialStatistics(req);
		Calendar calendar = Calendar.getInstance();
		final SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月");
		Date date = new Date();
		
		@SuppressWarnings("unchecked")
		List<Map<String, Object>> ruchishijianResult = (List<Map<String, Object>>) pagedata.getData().get("入池");
		@SuppressWarnings("unchecked")
		List<Map<String, Object>> zhuanshangjiSumResult = (List<Map<String, Object>>) pagedata.getData().get("转商机");
		
		for(int i = 0; i < 12; i++) {
			calendar.setTime(date);
			calendar.add(Calendar.MONTH, -i);
			String dateString = sdf.format(calendar.getTime());
			
			
			boolean flag = false;
			for(Map<String, Object> m : ruchishijianResult) {
				if(dateString.equals(m.get("time"))) {
					flag = true;
					break;
				}
			}
			if(!flag) {
				Map<String, Object> item = new LinkedHashMap<String,Object>();
				item.put("time", dateString);
				item.put("count", 0);
				ruchishijianResult.add(item);
			}
			flag = false;
			for(Map<String, Object> m : zhuanshangjiSumResult) {
				if(dateString.equals(m.get("time"))) {
					flag = true;
					break;
				}
			}
			if(!flag) {
				Map<String, Object> item = new LinkedHashMap<String,Object>();
				item.put("time", dateString);
				item.put("count", 0);
				zhuanshangjiSumResult.add(item);
			}
		}
		Comparator<Map<String, Object>> comparator = new Comparator<Map<String, Object>>() {
			@Override
			public int compare(Map<String, Object> o1, Map<String, Object> o2) {
				try {
					return sdf.parse((String)o1.get("time")).compareTo(sdf.parse((String)o2.get("time")));
				} catch (ParseException e) {
					return 0;
				}
			}
		};
		Collections.sort(ruchishijianResult, comparator);
		Collections.sort(zhuanshangjiSumResult, comparator);
		return pagedata;
	}
	
	public PageData getClueReactionStatistics(DataReq req) {
		return impl.getClueReactionStatistics(req);
	}
	
	public PageData getEmployeeFollowUpList(DataReq req) {
		return impl.getEmployeeFollowUpList(req);
	}
	
}
